﻿#include "astyleoptiondialog.h"

AStyleOptionDialog::AStyleOptionDialog(QWidget *parent) : QDialog(parent)   //构造函数定义，将外部图像作为引用传入
{
    this->setFixedSize(532, 280);
    this->setWindowTitle(tr("AStyle Options"));
    QVBoxLayout *mainVLayout = new QVBoxLayout; //定义垂直布局器
    this->setLayout(mainVLayout);//将mainVLayout设定为本窗口的布局器

    deleteExtraLinesCheckBox = new QCheckBox(tr("Delete extra empty lines"));
    insertSpaceCheckBox = new QCheckBox(tr("Insert space around operators"));
    autoFormatCheckBox = new QCheckBox(tr("Automatically format current file before saving"));
    bracketStyleComboBox = new QComboBox;
    indentStyleComboBox = new QComboBox;
    indentWidthSpinBox = new QSpinBox;
    maxLineWidthSpinBox = new QSpinBox;

    maxLineWidthCheckBox = new QCheckBox(tr("Max line width:"));

    classesCheckBox = new QCheckBox("Classes");
    namespacesCheckBox = new QCheckBox("Namespace");
    switchesCheckBox = new QCheckBox("Switches");
    labelsCheckBox = new QCheckBox("Labels");
    casesCheckBox = new QCheckBox("Cases");
    commentsCheckBox = new QCheckBox("Comments");

    okButton = new QPushButton(tr("OK")); //定义OK按钮
    defaultButton = new QPushButton(tr("Default")); //定义Default按钮
    cancelButton = new QPushButton(tr("Cancel")); //定义Cancel按钮

    QGroupBox *optionGroupBox = new QGroupBox(tr("Options"));
    QHBoxLayout *optionsHLayout = new QHBoxLayout;
    optionsHLayout->setSpacing(20);
    optionGroupBox->setLayout(optionsHLayout);

    QStringList bracketStyleList{"Allman", "Java", "K&&R", "Stroustrup", "Whitesmith", "Banner", "GNU", "Linux", "Horstmann", "OTBS", "Pico", "Lisp"};
    bracketStyleComboBox->addItems(bracketStyleList);

    QStringList indentStyleList{"tab", "spaces", "force-tab"};
    indentStyleComboBox->addItems(indentStyleList);

    indentWidthSpinBox->setRange(2, 20); //设定范围为2到20
    indentWidthSpinBox->setValue(4); //设定当前值为4个空格宽度
    maxLineWidthSpinBox->setRange(50, 200); //设定范围为50到200
    maxLineWidthSpinBox->setValue(120); //设定当前值为120

    QFormLayout *formLayout1 = new QFormLayout;
    formLayout1->setSpacing(8);
    formLayout1->addRow(tr("Bracket style: "), bracketStyleComboBox);
    formLayout1->addRow(tr("Indent style: "), indentStyleComboBox);
    formLayout1->addRow(tr("Indent width:   "), indentWidthSpinBox);

    maxLineWidthCheckBox->setChecked(false);
    maxLineWidthSpinBox->setEnabled(false);
    connect(maxLineWidthCheckBox, &QCheckBox::checkStateChanged, this, &AStyleOptionDialog::maxLineWidthCheckBoxStateChanged);

    QHBoxLayout *maxLineWidthHLayout = new QHBoxLayout;
    maxLineWidthHLayout->addWidget(maxLineWidthCheckBox);
    maxLineWidthHLayout->addWidget(maxLineWidthSpinBox);
    formLayout1->addRow(maxLineWidthHLayout);
    QHBoxLayout *hLayout00 = new QHBoxLayout;
    hLayout00->addWidget(insertSpaceCheckBox);
    formLayout1->addRow(hLayout00);

    QFormLayout *formLayout2 = new QFormLayout;
    formLayout2->setSpacing(12);
    QHBoxLayout *hLayout0 = new QHBoxLayout;
    hLayout0->addWidget(deleteExtraLinesCheckBox);
    formLayout2->addRow(hLayout0);
    formLayout2->addWidget(new QLabel(tr("<p style=\"color:rgb(0,128,255);\">Indent the following code:")));
    QHBoxLayout *hLayout1 = new QHBoxLayout;
    hLayout1->addWidget(classesCheckBox);
    hLayout1->addWidget(namespacesCheckBox);
    QHBoxLayout *hLayout2 = new QHBoxLayout;
    hLayout2->addWidget(switchesCheckBox);
    hLayout2->addWidget(labelsCheckBox);
    QHBoxLayout *hLayout3 = new QHBoxLayout;
    hLayout3->addWidget(casesCheckBox);
    hLayout3->addWidget(commentsCheckBox);

    classesCheckBox->setChecked(true);
    namespacesCheckBox->setChecked(true);
    switchesCheckBox->setChecked(true);
    casesCheckBox->setChecked(true);
    labelsCheckBox->setChecked(true);
    commentsCheckBox->setChecked(true);

    formLayout2->addRow(hLayout1);
    formLayout2->addRow(hLayout2);
    formLayout2->addRow(hLayout3);

    optionsHLayout->addLayout(formLayout1);
    optionsHLayout->addLayout(formLayout2);

    QHBoxLayout *okCancelHLayout = new QHBoxLayout;//定义水平布局器okCancelHLayout
    okCancelHLayout->addStretch();//添加弹簧
    okCancelHLayout->addWidget(okButton);//添加OK按钮到
    okCancelHLayout->addWidget(defaultButton);//添加Default按钮到
    okCancelHLayout->addWidget(cancelButton);//添加Cancel按钮到
    okCancelHLayout->addStretch();//添加弹簧

    mainVLayout->addWidget(optionGroupBox);
    mainVLayout->addWidget(autoFormatCheckBox);
    mainVLayout->addLayout(okCancelHLayout);

    connect(okButton, &QPushButton::clicked, this, &AStyleOptionDialog::okButtonClicked);//OK按钮点击和处理函数的连接
    connect(defaultButton, &QPushButton::clicked, this, &AStyleOptionDialog::defaultButtonClicked);//Default按钮点击和处理函数的连接
    connect(cancelButton, &QPushButton::clicked, this, &AStyleOptionDialog::cancelButtonClicked);//Cancel按钮点击和处理函数的连接
}

void AStyleOptionDialog::showEvent(QShowEvent* event)
{
    QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QSettings setting(appDataPath + "/Setting.ini", QSettings::IniFormat);

    int bracketStyleComboBoxCurrentIndex = setting.value("BracketStyle", 0).toInt();
    bracketStyleComboBox->setCurrentIndex(bracketStyleComboBoxCurrentIndex);

    QString indentStyleComboBoxCurrentText = setting.value("IndentStyle", "tab").toString();
    indentStyleComboBox->setCurrentText(indentStyleComboBoxCurrentText);

    int tabSpaceNumber = setting.value("AStyleIndentWidth", 4).toInt();
    if (tabSpaceNumber < 2) tabSpaceNumber = 2;
    else if (tabSpaceNumber > 20) tabSpaceNumber = 20;
    indentWidthSpinBox->setValue(tabSpaceNumber);

    bool maxLineWidthCheckBoxIsChecked = setting.value("MaxLineWidthCheckBox", false).toBool();
    maxLineWidthCheckBox->setChecked(maxLineWidthCheckBoxIsChecked);

    int maxLineWidthSpinBoxValue = setting.value("MaxLineWidthSpinBox", 120).toInt();
    maxLineWidthSpinBox->setValue(maxLineWidthSpinBoxValue);

    bool classesCheckBoxIsChecked = setting.value("ClassesCheckBox", true).toBool();
    classesCheckBox->setChecked(classesCheckBoxIsChecked);

    bool namespacesCheckBoxIsChecked = setting.value("NamespacesCheckBox", true).toBool();
    namespacesCheckBox->setChecked(namespacesCheckBoxIsChecked);

    bool switchesCheckBoxIsChecked = setting.value("SwitchesCheckBox", true).toBool();
    switchesCheckBox->setChecked(switchesCheckBoxIsChecked);

    bool labelsCheckBoxIsChecked = setting.value("LabelsCheckBox", true).toBool();
    labelsCheckBox->setChecked(labelsCheckBoxIsChecked);

    bool casesCheckBoxIsChecked = setting.value("CasesCheckBox", true).toBool();
    casesCheckBox->setChecked(casesCheckBoxIsChecked);

    bool commentsCheckBoxIsChecked = setting.value("CommentsCheckBox", true).toBool();
    commentsCheckBox->setChecked(commentsCheckBoxIsChecked);

    bool autoFormatCheckBoxIsChecked = setting.value("AutoFormatCheckBox", true).toBool();
    autoFormatCheckBox->setChecked(autoFormatCheckBoxIsChecked);

    bool deleteExtraLinesCheckBoxIsChecked = setting.value("DeleteExtraLinesCheckBox", true).toBool();
    deleteExtraLinesCheckBox->setChecked(deleteExtraLinesCheckBoxIsChecked);

    bool insertSpaceCheckBoxIsChecked = setting.value("InsertSpaceCheckBox", true).toBool();
    insertSpaceCheckBox->setChecked(insertSpaceCheckBoxIsChecked);
}

void AStyleOptionDialog::maxLineWidthCheckBoxStateChanged(Qt::CheckState state)
{
    maxLineWidthSpinBox->setEnabled(state==Qt::Checked);
}

void AStyleOptionDialog::okButtonClicked()
{
    QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QSettings setting(appDataPath + "/Setting.ini", QSettings::IniFormat);

    setting.setValue("BracketStyle", bracketStyleComboBox->currentIndex());
    setting.setValue("IndentStyle", indentStyleComboBox->currentText());
    setting.setValue("AStyleIndentWidth", indentWidthSpinBox->value());
    setting.setValue("MaxLineWidthCheckBox", maxLineWidthCheckBox->isChecked());
    setting.setValue("MaxLineWidthSpinBox", maxLineWidthSpinBox->value());

    setting.setValue("ClassesCheckBox", classesCheckBox->isChecked());
    setting.setValue("NamespacesCheckBox", namespacesCheckBox->isChecked());
    setting.setValue("SwitchesCheckBox", switchesCheckBox->isChecked());
    setting.setValue("LabelsCheckBox", labelsCheckBox->isChecked());
    setting.setValue("CasesCheckBox", casesCheckBox->isChecked());
    setting.setValue("CommentsCheckBox", commentsCheckBox->isChecked());

    setting.setValue("AutoFormatCheckBox", autoFormatCheckBox->isChecked());
    setting.setValue("DeleteExtraLinesCheckBox", deleteExtraLinesCheckBox->isChecked());
    setting.setValue("InsertSpaceCheckBox", insertSpaceCheckBox->isChecked());

    this->accept();//OK按钮点击以后发出Accepted返回值
}

void AStyleOptionDialog::defaultButtonClicked()
{
    bracketStyleComboBox->setCurrentIndex(0);
    indentStyleComboBox->setCurrentIndex(0);
    indentWidthSpinBox->setValue(4);
    maxLineWidthCheckBox->setChecked(false);
    maxLineWidthSpinBox->setValue(120);
    classesCheckBox->setChecked(true);
    namespacesCheckBox->setChecked(true);
    switchesCheckBox->setChecked(true);
    labelsCheckBox->setChecked(true);
    casesCheckBox->setChecked(true);
    commentsCheckBox->setChecked(true);
    autoFormatCheckBox->setChecked(true);
    deleteExtraLinesCheckBox->setChecked(true);
    insertSpaceCheckBox->setChecked(true);
}

void AStyleOptionDialog::cancelButtonClicked()
{
    this->reject();//Cancel按钮点击以后发出Rejected返回值
}
